package com.weixue.service.impl;

import com.weixue.common.ServiceResultEnum;
import com.weixue.controller.vo.NongFuMallSearchGoodsVO;
import com.weixue.dao.NongFuMallGoodsMapper;
import com.weixue.entity.NongFuMallGoods;
import com.weixue.service.NongFuMallGoodsService;
import com.weixue.utils.BeanUtil;
import com.weixue.utils.PageQueryUtil;
import com.weixue.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Day29
 *      商品添加业务逻辑层实现类
 */
@Service
public class NongFuMallGoodsServiceImpl implements NongFuMallGoodsService {

    @Autowired
    private NongFuMallGoodsMapper goodsMapper;

    /**
     * 1.添加商品
     */
    @Override
    public String saveNongFuMallGoods(NongFuMallGoods goods) {
        if (goodsMapper.insertSelective(goods) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    /**
     * Day30
     *      2.修改商品信息
     */
    @Override
    public String updateNongFuMallGoods(NongFuMallGoods goods) {
        NongFuMallGoods temp = goodsMapper.selectByPrimaryKey(goods.getGoodsId());
        if (temp == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        goods.setUpdateTime(new Date());
        if (goodsMapper.updateByPrimaryKeySelective(goods) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    /**
     * Day30
     *      3.获取商品详情
     */
    @Override
    public NongFuMallGoods getNongFuMallGoodsById(Long id) {
        return goodsMapper.selectByPrimaryKey(id);
    }

    /**
     * Day30
     * 4.后台分页
     */
    @Override
    public PageResult getNongFuMallGoodsPage(PageQueryUtil pageUtil) {
        List<NongFuMallGoods> goodsList = goodsMapper.findNongFuMallGoodsList(pageUtil);
        int total = goodsMapper.getTotalNongFuMallGoods(pageUtil);
        PageResult pageResult = new PageResult(goodsList, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    /**
     * Day30
     * 5.批量修改销售状态(上架下架)
     */
    @Override
    public Boolean batchUpdateSellStatus(Long[] ids, int sellStatus) {
        return goodsMapper.batchUpdateSellStatus(ids, sellStatus) > 0;
    }

    @Override
    public void batchSaveNongFuMallGoods(List<NongFuMallGoods> nongFuMallGoodsList) {
        if (!CollectionUtils.isEmpty(nongFuMallGoodsList)) {
            goodsMapper.batchInsert(nongFuMallGoodsList);
        }
    }

    /**
     * Day35
     * 1.商品搜索
     */
    @Override
    public PageResult searchNongFuMallGoods(PageQueryUtil pageUtil) {
        List<NongFuMallGoods> goodsList = goodsMapper.findNongFuMallGoodsListBySearch(pageUtil);
        int total = goodsMapper.getTotalNongFuMallGoodsBySearch(pageUtil);
        List<NongFuMallSearchGoodsVO> nongFuMallSearchGoodsVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(goodsList)) {
            nongFuMallSearchGoodsVOS = BeanUtil.copyList(goodsList, NongFuMallSearchGoodsVO.class);
            for (NongFuMallSearchGoodsVO nongFuMallSearchGoodsVO : nongFuMallSearchGoodsVOS) {
                String goodsName = nongFuMallSearchGoodsVO.getGoodsName();
                String goodsIntro = nongFuMallSearchGoodsVO.getGoodsIntro();
                // 字符串过长导致文字超出的问题
                if (goodsName.length() > 28) {
                    goodsName = goodsName.substring(0, 28) + "...";
                    nongFuMallSearchGoodsVO.setGoodsName(goodsName);
                }
                if (goodsIntro.length() > 30) {
                    goodsIntro = goodsIntro.substring(0, 30) + "...";
                    nongFuMallSearchGoodsVO.setGoodsIntro(goodsIntro);
                }
            }
        }
        PageResult pageResult = new PageResult
                (nongFuMallSearchGoodsVOS, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }
}
